/** @noSelfInFile **/
import {LocModel} from "../../../base/LocModel";
import {Logger} from "../../../config/Logger";
import {HandleModel} from "../../../adapterModel/model1/HandleModel";
import {RectModel} from "../../../adapterModel/model2/RectModel";
import {EffectModel} from "../../../adapterModel/model2/EffectModel";
import {TexttagModel} from "../../../adapterModel/model2/TexttagModel";
import {UnitModel} from "../../../adapterModel/model3/UnitModel";
import {ItemModel} from "../../../adapterModel/model3/ItemModel";
import {PlayerModel} from "../../../adapterModel/model4/PlayerModel";


export enum ModelType {
    unit = 1,
    effect,
    item,
    sound,
    text,
    rect,
    trigger,
    other,
}

export class ModelCenter {
    modelMap: {
        [ModelType.unit]: { [key: string]: UnitModel },
        [ModelType.effect]: { [key: string]: EffectModel },
        [ModelType.item]: { [key: string]: ItemModel },
        [ModelType.text]: { [key: string]: TexttagModel },
        [ModelType.rect]: { [key: string]: RectModel },
        [ModelType.other]: { [key: string]: HandleModel },
    } = {
        [ModelType.unit]: {},
        [ModelType.effect]: {},
        [ModelType.item]: {},
        [ModelType.text]: {},
        [ModelType.rect]: {},
        [ModelType.other]: {},
    };

    addListen = (id: string, handleModel: HandleModel, type: ModelType) => {
        const models = this.modelMap[type];
        if (id) {
        } else {
            return false;
        }
        if (models[id]) {
            Logger.toastError(`name: ${handleModel}}`)
            Logger.toastError(`type: ${type} id重复:${id}`)
            return false;
        }
        models[id] =  handleModel;
        return true;
    }
    removeListen = (handleModel: HandleModel, type: ModelType) => {
        this.modelMap[type][handleModel.getHandleId()] = undefined
    }
    getModel = (id: string, type: ModelType) => {
        if (id) {
            return this.modelMap[type][id];
        }
    }
}

export const modelCenter = new ModelCenter();
export let modelAdapter: { adapter: IModelAdapter } = { adapter: undefined };

export interface IModelAdapter {
    initRectUnit(rect: RectModel, fun: (unit: UnitModel) => UnitModel): UnitModel[];
    mapUnitInRect(rect: RectModel, fun: (unitModel: UnitModel) => void): void;
    mapUnitInRange(loc: LocModel, radius: number, fun: (unit: UnitModel) => void): void;
    mapItemsInRange(loc: LocModel, radius: number, callBack: (item: ItemModel) => void): void
    mapItemsInRect(rect: RectModel, callBack: (item: ItemModel) => void): void
    mapUnitItems(unit: UnitModel, fun: (item: ItemModel, index: number) => any): void;
    mapPlayerSelected(player: PlayerModel, fun: (unit: UnitModel) => void): void
    rectContainsUnit(rect: RectModel, unit: UnitModel): boolean
}
